<?php
/**
 * 基准测试类
 *
 * @author fakis738@gmail.com
 * @link http://www.woophp.com/manual/class/benchmark.lib.php
 * @version $id benchmark.lib.php v0.1 2011-12-03$
 */
class Benchmark_lib
{
    var $marker = array();

    function __construct($param=NULL)
    {
    }
    
    /**
     * 标记
     * @access public
     * @param string $name
     */
    function mark($name)
    {
        $this->marker[$name]['time'] = microtime();
        $this->marker[$name]['memory'] = memory_get_usage();
    }

    /**
     * 执行时间分析
     * @access public
     * @param string $start
     * @param string $end
     * @param int $decimals
     */
    function usage_time($start = '', $end = '', $decimals = 6)
    {
        if(!isset($this->marker[$start]) && !isset($this->marker[$end]))
        {
            return 'error usage_time';
        }
        list($sm, $ss) = explode(' ', $this->marker[$start]['time']);
        list($em, $es) = explode(' ', $this->marker[$end]['time']);
        return number_format(($em + $es) - ($sm + $ss), $decimals);
    }

    /**
     * 使用内存分析
     * @access public
     * @param string $start
     * @param string $end
     * @param int $decimals
     */
    function usage_momery($start = '', $end = '', $decimals = 6)
    {
        if(!isset($this->marker[$start]) && !isset($this->marker[$end]))
        {
            return 'error usage_memory';
        }
        $sm = $this->marker[$start]['momery'];
        $em = $this->marker[$end]['momery'];
        return number_format(($em - $sm) / 1024 / 1024, $decimals);
    }
}
